app.controller('NewCampusModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var schoolname, schoolshort_name, schooladdrs;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {
                    schoolname = $scope.oo.name;
                    schoolshort_name = $scope.oo.short_name;
                 
                    schooladdrs = $scope.oo.province + ',' + $scope.oo.city + ',' + $scope.oo.district +',' + $scope.oo.detaiaddr;          
                                      
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'newcampusModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-school/create",
                        {
                            'name': schoolname,
                            'short_name': schoolshort_name,
                            'address': schooladdrs,      
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                     // console.log(data);
                      if(data.code==0&&data.msg=='学校创建成功'){
                        alert(data.msg);
                        $window.location.reload('app/sys/campus');                      

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])

app.controller('EditCampusModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var schoolname, schoolshort_name, province,city,district,detaiaddr,schoolid;
            var school_name,school_shortname, school_addrs;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.oo.name = schoolname;
                $scope.oo.short_name = schoolshort_name;
              
                $scope.oo.province = province;
                $scope.oo.city = city;
                $scope.oo.district = district;
                $scope.oo.detaiaddr = detaiaddr;              
                
                

                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {
                    school_name = $scope.oo.name;
                    school_shortname = $scope.oo.short_name;                 
                    school_addrs = $scope.oo.province + ',' + $scope.oo.city + ',' + $scope.oo.district + ',' + $scope.oo.detaiaddr;
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };

            $scope.editcampus = function(obj){
                //console.log(obj);
                schoolname = obj.name;
                schoolshort_name = obj.short_name;
                province = obj.addr.province;
                city = obj.addr.city;
                district = obj.addr.area;
                detaiaddr = obj.addr.location;
                schoolid = obj.school_id;
                // console.log(schooladdrs);
                $scope.open();

            }
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'editcampusModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-school/update",
                        {
                            'name': school_name,
                            'school_id': schoolid,
                            'short_name': school_shortname,
                            'address': school_addrs,      
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="更新学校信息成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/campus');

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])


app.controller('DelCampusModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$location','$localStorage',
        function ($scope, $modal, $log, $http, $window, $location,$localStorage) {

            $scope.items = ['item1', 'item2', 'item3'];
            var campus_id;            
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

                $scope.oo = {};

                $scope.items = items;               
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {

                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };


            $scope.delcampus = function (aid) {
                //console.log(aid);
                campus_id = aid;             
                $scope.open();
            }

            $scope.open = function (size) {

                var modalInstance = $modal.open({
                    templateUrl: 'delcampusModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;

                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $http.post(
                        $scope.app.backendurl+"tbl-school/delete",
                        {
                           
                            'school_id': campus_id,                             
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="删除成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/campus');

                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })


                }, function () {
                    $log.info('Modal dismissed at: ' + new Date());
                });
            };
        }])